package com.superme.freemarkerquickstart.service.Impl;

import com.superme.freemarkerquickstart.controller.Data.Person;
import com.superme.freemarkerquickstart.controller.Data.ResultData;
import com.superme.freemarkerquickstart.service.PersonService;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class PersonServiceImpl implements PersonService {
    RestTemplate restTemplate = new RestTemplate();

    //根据条件过滤人员信息
    public HashMap getAllPersons(String searchText, int pageNum, int pageSize, String sortOrder, String sortName) {
        Map<String, Object> urlVariables = new HashMap<String, Object>();
        urlVariables.put("searchText", searchText);
        urlVariables.put("pageNumber", pageNum);
        urlVariables.put("pageSize", pageSize);
        urlVariables.put("sortOrder", sortOrder);
        urlVariables.put("sortName", sortName);
        /*
        //两种传参的写法
        Object result = restTemplate.getForObject("http://localhost:8080/api/v1/persons?name={name}&nickname={nickname}&page={page}&per_page={per_page}&sortby={sortby}&order={order}", String.class,
                "","",0,10,"name","asc");*/
        //通过restTemplate调用restapi get操作
        HashMap result = restTemplate.getForObject("http://localhost:8080/api/v2/persons?searchText={searchText}&pageNumber={pageNumber}&pageSize={pageSize}&sortName={sortName}&sortOrder={sortOrder}", HashMap.class, urlVariables);
        return result;
    } //通过restTemplate调用restapi

    //根据ID删除人员信息
    @Override
    public ResultData deletePersonsById(Integer Id) {
        ResultData resultData = new ResultData();
        try {
            //调用restapi delete操作
            restTemplate.delete("http://localhost:8080/api/v1/persons/{Id}", Id);
        } catch (Exception e) {
            resultData.setMessage("删除失败!");
            resultData.setErrormsg(e.getMessage());
            resultData.setIs_success(false);
            return resultData;
        }
        resultData.setMessage("删除成功!");
        resultData.setIs_success(true);
        return resultData;
    }

    //新增人员
    @Override
    public ResultData addPerson(Person person) {
        ResultData resultData = new ResultData();
        try {
            //调用restapi post操作
            restTemplate.postForLocation("http://localhost:8080/api/v1/persons", person);
        } catch (Exception e) {
            resultData.setMessage("新增失败!");
            resultData.setErrormsg(e.getMessage());
            resultData.setIs_success(false);
            return resultData;
        }
        resultData.setMessage("新增成功!");
        resultData.setIs_success(true);
        return resultData;
    }

    //根据ID获取人员信息
    @Override
    public Person findPersonById(Integer Id) {
        return restTemplate.getForObject("http://localhost:8080/api/v1/persons/{Id}", Person.class, Id);
    }

    //更新人员
    @Override
    public ResultData updatePerson(Integer personid, Person person) {
        ResultData resultData = new ResultData();
        try {
            //调用restapi put操作
            restTemplate.put("http://localhost:8080/api/v1/persons/" + personid, person);
        } catch (Exception e) {
            resultData.setMessage("更新失败!");
            resultData.setErrormsg(e.getMessage());
            resultData.setIs_success(false);
            return resultData;

        }
        resultData.setMessage("更新成功!");
        resultData.setIs_success(true);
        return resultData;
    }
}
